package com.wisemoresystem.seal.action.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_bank_verify;
import com.wisemoresystem.seal.model.t_user;
import com.wisemoresystem.seal.service.BankService;
import com.wisemoresystem.seal.service.RoleService;
import com.wisemoresystem.seal.service.UserService;
import com.wisemoresystem.seal.service.VerifyService;
import com.wisemoresystem.seal.util.json.JsonTools;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 人员审核管理
 * 
 * @author Administrator
 *
 */
@Controller("ixUserVerify")
@Scope("prototype")
@RequestMapping("/ix/user/verify")
public class UserVerifyAction extends ProjBaseAction {

	@Autowired
	private UserService userService;
	@Autowired
	private BankService bService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private VerifyService vService;

	/**
	 * 人员审核管理列表
	 * 
	 * @param srh_name
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String srh_name) {

		List<Map<String, Object>> userList = vService.selectUserVerify(
				srh_name, page);

		ModelAndView mv = new ModelAndView("/jsp/user/verify/list");
		mv.addObject("userList", userList);
		return mv;
	}

	/**
	 * 审核详细信息
	 * 
	 * @param verify_id
	 *            审核id
	 * @return
	 */
	@RequestMapping("/get_result")
	public ModelAndView get_result(String verify_id) {
		System.out.println(verify_id + "[pppppp");
		t_bank_verify verify = oc.findObjById(verify_id, t_bank_verify.class);
		Map<String, Object> maps1 = new HashMap<String, Object>();
		Map<String, Object> maps2 = new HashMap<String, Object>();
		String before = verify.getBefore_v();
		String after = verify.getAfter_v();
		maps1 = JsonTools.jsonStrToMap(before);
		if (ChkTools.isNotNull(after.trim())) {
			maps2 = JsonTools.jsonStrToMap(after);
		} else {
			after = null;
		}

		System.out.println(maps2);
		List<Map<String, Object>> role = roleService.selectRoleList(null);
		t_user user = oc.findObjById(maps1.get("id") + "", t_user.class);
		t_bank bank = oc.findObjById(user.getBank_id(), t_bank.class);
		String bankname = null;
		if (ChkTools.isNotNull(maps2)) {
			t_bank bank2 = oc.findObjById(maps2.get("bank_id").toString(),
					t_bank.class);
			bankname = bank2.getName();
		}

		ModelAndView mv = new ModelAndView("/jsp/user/verify/get_result");
		mv.addObject("user", user);
		mv.addObject("bank", bank);
		mv.addObject("role", role);
		mv.addObject("verify", verify);
		mv.addObject("after", maps2);
		mv.addObject("bank2", bankname);
		return mv;
	}

	/**
	 * 审核修改
	 * 
	 * @param verify
	 *            审核信息
	 * @param verify_id
	 *            审核id
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(t_bank_verify verify, String verify_id) {

		t_bank_verify ver = vService
				.findObjById(verify_id, t_bank_verify.class);
		ver.setFlag(verify.getFlag());
		ver.setNote(verify.getNote());
		ver.setUser_id(WebTools.getCurrentUserId());
		vService.updateObj(ver);

		Map<String, Object> maps1 = new HashMap<String, Object>();
		Map<String, Object> maps2 = new HashMap<String, Object>();
		String before = ver.getBefore_v();
		maps1 = JsonTools.jsonStrToMap(before);
		t_user userDb = oc.findObjById(maps1.get("id") + "", t_user.class);
		if ("通过".equals(ver.getFlag())) {
			if ("修改待审核".equals(ver.getFlag_note())) {
				String after = ver.getAfter_v();
				maps2 = JsonTools.jsonStrToMap(after);
				userDb.setIs_auth(maps2.get("is_auth") + "");
				userDb.setBank_id(maps2.get("bank_id") + "");
				userDb.setBoeing(maps2.get("boeing") + "");
				userDb.setCode(maps2.get("code") + "");
				userDb.setLogin_type(maps2.get("login_type") + "");
				userDb.setNote(maps2.get("note") + "");
				userDb.setP_id(maps2.get("p_id") + "");
				userDb.setRole_id(maps2.get("role_id") + "");
				userDb.setPhone(maps2.get("phone") + "");
				userDb.setUname(maps2.get("uname") + "");
				userDb.setType(maps2.get("type") + "");
				userDb.setLogin_type(maps2.get("login_type") + "");
				userDb.setUser_level(maps2.get("user_level") + "");
				userDb.setUser_flag("正常");
				userDb.setJob_code(maps2.get("job_code") + "");
				oc.updateObj(userDb);
			} else if ("停用待审核".equals(ver.getFlag_note())) {
				userDb.setUser_flag("停用");
				oc.updateObj(userDb);
			} else if ("启用待审核".equals(ver.getFlag_note())) {
				userDb.setUser_flag("正常");
				oc.updateObj(userDb);
			} else if ("删除待审核".equals(ver.getFlag_note())) {
				userDb.setUser_flag("停用");
				oc.delObjById(maps1.get("id") + "", t_user.class);
			} else if ("新建人员审核".equals(ver.getFlag_note())) {
				userDb.setUser_flag("正常");
				oc.updateObj(userDb);
			}
			logT.logH("通过人员状态修改", t_user.class, userDb.get_id());
		} else {
			if ("新建人员审核".equals(ver.getFlag_note())) {
				oc.delObjById(userDb.get_id(), t_user.class);
			} else if ("停用待审核".equals(ver.getFlag_note())) {
				userDb.setUser_flag("正常");
				oc.updateObj(userDb);
			} else if ("启用待审核".equals(ver.getFlag_note())) {
				userDb.setUser_flag("停用");
				oc.updateObj(userDb);
			} else {
				userDb.setUser_flag("正常");
				oc.updateObj(userDb);
			}
			logT.logH("拒绝人员状态修改", t_user.class, userDb.get_id());
		}

		super.setTipMsg("操作成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	/**
	 * 批量审核保存
	 * 
	 * @param verifyvalue
	 *            审核值
	 * @param verify
	 *            审核信息
	 * @return
	 */
	@RequestMapping("/batch_save")
	public ModelAndView batch_save(String verifyvalue, t_bank_verify verify) {

		if (ChkTools.isNotNull(verifyvalue)) {
			String[] ev = verifyvalue.split("@@");
			for (String fv : ev) {
				t_bank_verify ver = vService.findObjById(fv,
						t_bank_verify.class);
				ver.setFlag(verify.getFlag());
				ver.setNote(verify.getNote());
				ver.setUser_id(WebTools.getCurrentUserId());
				vService.updateObj(ver);

				Map<String, Object> maps1 = new HashMap<String, Object>();
				Map<String, Object> maps2 = new HashMap<String, Object>();
				String before = ver.getBefore_v();
				maps1 = JsonTools.jsonStrToMap(before);
				t_user userDb = oc.findObjById(maps1.get("id") + "",
						t_user.class);
				if ("通过".equals(ver.getFlag())) {
					if ("修改待审核".equals(ver.getFlag_note())) {
						String after = ver.getAfter_v();
						maps2 = JsonTools.jsonStrToMap(after);
						userDb.setIs_auth(maps2.get("is_auth") + "");
						userDb.setBank_id(maps2.get("bank_id") + "");
						userDb.setBoeing(maps2.get("boeing") + "");
						userDb.setCode(maps2.get("code") + "");
						userDb.setLogin_type(maps2.get("login_type") + "");
						userDb.setNote(maps2.get("note") + "");
						userDb.setP_id(maps2.get("p_id") + "");
						userDb.setRole_id(maps2.get("role_id") + "");
						userDb.setPhone(maps2.get("phone") + "");
						userDb.setUname(maps2.get("uname") + "");
						userDb.setType(maps2.get("type") + "");
						userDb.setLogin_type(maps2.get("login_type") + "");
						userDb.setUser_level(maps2.get("user_level") + "");
						userDb.setUser_flag("正常");
						oc.updateObj(userDb);
					} else if ("停用待审核".equals(ver.getFlag_note())) {
						userDb.setUser_flag("停用");
						oc.updateObj(userDb);
					} else if ("启用待审核".equals(ver.getFlag_note())) {
						userDb.setUser_flag("正常");
						oc.updateObj(userDb);
					} else if ("删除待审核".equals(ver.getFlag_note())) {
						userDb.setUser_flag("停用");
						oc.delObjById(maps1.get("id") + "", t_user.class);
					} else if ("新建人员审核".equals(ver.getFlag_note())) {
						userDb.setUser_flag("正常");
						oc.updateObj(userDb);
					}
					logT.logH("通过人员状态修改", t_user.class, userDb.get_id());
				} else {
					if ("新建人员审核".equals(ver.getFlag_note())) {
						oc.delObjById(userDb.get_id(), t_user.class);
					} else if ("停用待审核".equals(ver.getFlag_note())) {
						userDb.setUser_flag("正常");
						oc.updateObj(userDb);
					} else if ("启用待审核".equals(ver.getFlag_note())) {
						userDb.setUser_flag("停用");
						oc.updateObj(userDb);
					} else {
						userDb.setUser_flag("正常");
						oc.updateObj(userDb);
					}
					logT.logH("拒绝人员状态修改", t_user.class, userDb.get_id());
				}

			}
			super.setTipMsg("操作成功！", Tip.Type.success);
		} else {
			super.setTipMsg("请选择！", Tip.Type.success);
		}

		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;

	}

}
